<?php

namespace backend\modules\home\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use backend\modules\home\models\Booklist;

/**
 * BooklistSearch represents the model behind the search form of `backend\modules\home\models\Booklist`.
 */
class BooklistSearch extends Booklist
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'cid', 'parent_id', 'number', 'cont_num', 'click_num', 'zan_num', 'create_at', 'sort'], 'integer'],
            [['type', 'front_title', 'backend_title', 'thumb_img', 'intro', 'is_recommend', 'is_hot', 'is_over', 'is_advert', 'is_free', 'mark', 'remark', 'date', 'status'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = Booklist::find();

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'sort' => [
                'defaultOrder' => [
                    'id' => SORT_DESC,
                ]
            ],
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
            'cid' => $this->cid,
            'parent_id' => $this->parent_id,
            'number' => $this->number,
            'cont_num' => $this->cont_num,
            'click_num' => $this->click_num,
            'zan_num' => $this->zan_num,
            'create_at' => $this->create_at,
        ]);

        $query->andFilterWhere(['like', 'type', $this->type])
            ->andFilterWhere(['like', 'front_title', $this->front_title])
            ->andFilterWhere(['like', 'backend_title', $this->backend_title])
            ->andFilterWhere(['like', 'thumb_img', $this->thumb_img])
            ->andFilterWhere(['like', 'intro', $this->intro])
            ->andFilterWhere(['like', 'is_recommend', $this->is_recommend])
            ->andFilterWhere(['like', 'is_hot', $this->is_hot])
            ->andFilterWhere(['like', 'is_over', $this->is_over])
            ->andFilterWhere(['like', 'is_advert', $this->is_advert])
            ->andFilterWhere(['like', 'is_free', $this->is_free])
            ->andFilterWhere(['like', 'mark', $this->mark])
            ->andFilterWhere(['like', 'remark', $this->remark])
            ->andFilterWhere(['like', 'date', $this->date])
            ->andFilterWhere(['like', 'status', $this->status]);

        return $dataProvider;
    }
}
